//
//  MVVMOperation.swift
//  UIKitExtentions
//
//  Created by Alean on 2023/3/20.
//

/*
 * 使用方式
 //model
 class WDSearchShopItem: WDBaseModel {
 }
 //viewmodel
 class WDSearchShopViewModel: WDBaseViewModel<WDSearchShopItem> {
    override var data: WDSearchShopItem? {
        didSet {
 
        }
    }
    override var list: [WDSearchShopItem]? {
        didSet {
            didChange?(nil)
        }
    }
    override func clicked(data: WDSearchShopItem?) {
 
    }
    override func initContent() {
        super.initContent()
        list = [WDSearchShopItem(),WDSearchShopItem(),WDSearchShopItem()]
    }
 }
 
 //view
 //class WDSearchShopCell: WDBaseCollectionCell<WDSearchShopItem> {}
 class WDSearchShopView: MVVMBaseView<WDSearchShopViewModel> {
     override var viewModel: WDSearchShopViewModel? {
         didSet {
             self.viewModel?.didChange = {[weak self] data in
                 self?.baseUpdate()
             }
         }
     }
     override func baseUpdate() {
         DispatchMain {
         }
     }
     override func initContent() {
        super.initContent()
     }
 }
 //controller
 class WDSearchShopViewController: BaseDefaultViewController {
     lazy var searchShopView = WDSearchShopView()
     lazy var searchShopViewModel = WDSearchShopViewModel()
    
     override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(searchShopView)
        searchShopView.bindViewModel(searchShopViewModel)
        searchShopView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview().offset(UETopLayoutGuide)
            make.bottom.equalToSuperview()
        }
    }
 }
 */
import Foundation

public protocol MVVMBaseViewModelProtocol {
    associatedtype T
    var data: T? { set get }
    var list: [T]? { set get }
    var didChange: ((T?) -> Void)? { set get }
    func clicked(data: T?)
    func baseUpdate()
}

public extension MVVMBaseViewModelProtocol {
    func clicked(data: T?) {
        
    }
    func baseUpdate() {
        
    }
}

open class MVVMBaseView<T: MVVMBaseViewModelProtocol>: UIView {
    open var viewModel: T? {
        didSet {
            viewModel?.baseUpdate()
        }
    }

    final public func bindViewModel(_ viewModel: T?) {
        self.viewModel = viewModel
    }
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
        initContent()
    }
    
    public required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    open func initContent()  {
    }
    
    open func baseUpdate() {
        fatalError("Must Override")
    }
}

@objc public protocol MVVMBaseModelProtocol: NSObjectProtocol {
    
}

open class WDBaseTableCell<T: MVVMBaseModelProtocol>: UITableViewCell, MVVMBaseViewModelProtocol {
//    public typealias T = PublicModelProtocol
    open var data: T? {
        didSet {
        }
    }
    open var list: [T]? {
        didSet {
        }
    }
    open var didChange: ((T?) -> Void)?
    open func clicked(data: T?) {
        
    }
    
    public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        initContent()
    }
    
    public required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    open func initContent() {

    }
}

open class WDBaseCollectionCell<T: MVVMBaseModelProtocol>: UICollectionViewCell, MVVMBaseViewModelProtocol {
//    public typealias T = PublicModelProtocol
    open var data: T? {
        didSet {
        }
    }
    open var list: [T]? {
        didSet {
        }
    }
    open var didChange: ((T?) -> Void)?
    open func clicked(data: T?) {
        
    }
//    open override class func awakeFromNib() {
//        super.awakeFromNib()
//        initContent()
//    }
    public override init(frame: CGRect) {
        super.init(frame: frame)
        initContent()
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    open func initContent()  {
    }
}

open class WDBaseCellView<T: MVVMBaseModelProtocol>: UIView, MVVMBaseViewModelProtocol {
//    public typealias T = PublicModelProtocol
    open var didChange: ((T?) -> Void)?
    open var data: T? {
        didSet {
        }
    }
    open var list: [T]? {
        didSet {
        }
    }
    open func clicked(data: T?) {
        
    }
    public override init(frame: CGRect) {
        super.init(frame: frame)
        initContent()
    }
    
    public required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    open func initContent()  {
    }
}

open class WDBaseViewModel<T: MVVMBaseModelProtocol>: NSObject, MVVMBaseViewModelProtocol {
    open var data: T?
    open var list: [T]?
    open var didChange: ((T?) -> Void)?
    open func clicked(data: T?) {
        
    }
    public var otherData: Any?

    public override init() {
        super.init()
        initContent()
    }
    
    public required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    open func initContent()  {
    }
}

open class WDBaseModel: NSObject, MVVMBaseModelProtocol {
    
}
